<!--
  * 搜索推荐推进楼盘
-->
<template>
  <a-form class="smart-query-form">
    <a-row class="smart-query-form-row">
      <a-form-item label="用户地址" class="smart-query-form-itme" name="certAddress">
        <a-input style="width: 380px" v-model:value="queryForm.certAddress" placeholder="请输入地址" />
      </a-form-item>

      <a-form-item label="查询距离" class="smart-query-form-itme" name="distance">
        <a-radio-group v-model:value="queryForm.distance" >
          <a-radio-button :value="1">1公里</a-radio-button>
          <a-radio-button :value="3">3公里</a-radio-button>
          <a-radio-button :value="5">5公里</a-radio-button>
        </a-radio-group>
      </a-form-item>

      <a-form-item class="smart-query-form-item smart-margin-left10">
        <a-button type="primary" @click="ajaxQuery" >
          <template #icon>
            <SearchOutlined />
          </template>
          查询
        </a-button>
        <a-button @click="resetQuery" class="smart-margin-left10">
          <template #icon>
            <ReloadOutlined />
          </template>
          重置
        </a-button>
        <a-button @click="initLP" type="primary"   v-privilege="'as:community:init'" class="smart-margin-left10">
          <template #icon>
            <CloudUploadOutlined />
          </template>
          初始化楼盘ES
        </a-button>
      </a-form-item>
    </a-row>
  </a-form>

  <a-card size="small" :bordered="false" :hoverable="true">
    <a-table
      :scroll="{ x: 1300 }"
      size="small"
      :dataSource="tableData"
      :columns="columns"
      rowKey="house_code"
      :pagination="false"
      :loading="tableLoading"
      bordered
    >
    </a-table>
  </a-card>
  <CheckCity ref="checkCityRef" />
</template>
<script setup lang="ts">
  import { reactive, ref, onMounted,computed } from 'vue';
  import { asApi } from '/@/api/addrstandard/as-api'
  import { smartSentry } from '/@/lib/smart-sentry';
  import CheckCity from '/@/views/addrstandard/lookup/check-city-modal.vue'
  import { message } from 'ant-design-vue';


  // --------------------------- 企业表格 列 ---------------------------

  const columns = ref([
  {
      title: '省',
      width: 120,
      dataIndex: 'proviceName',
      ellipsis: true,
    },
    {
      title: '市',
      width: 120,
      dataIndex: 'cityName',
      ellipsis: true,
    },
    {
      title: '区',
      dataIndex: 'districtName',
      fixed: 'right',
      width: 120,
    },
    {
      title: '楼盘编码',
      dataIndex: 'communityCode',
      minWidth: 180,
      // ellipsis: true,
    },
    {
      title: '楼盘名称',
      dataIndex: 'communityName',
      minWidth: 180,
      // ellipsis: true,
    },
    {
      title: '距离(米)',
      dataIndex: 'distanceMeters',
      width: 180,
    },
    {
      title: '百度坐标（经度）',
      dataIndex: 'lng',
      width: 180,
    },
    {
      title: '百度坐标（纬度）',
      dataIndex: 'lat',
      width: 180,
    },

  ]);

  // --------------------------- 查询 ---------------------------

  const queryFormState = {
    certAddress: undefined,
    distance: undefined,
  };
  const queryForm = reactive({ ...queryFormState });
  const tableLoading = ref(false);
  const tableData = ref([]);
  const checkCityRef =ref();

  function resetQuery() {
    Object.assign(queryForm, queryFormState);
    ajaxQuery();
  }

  async function ajaxQuery() {
    try {
      if(!queryForm.certAddress || !queryForm.distance)
        message.error("距离及地址信息不能为空！")
      else{
        tableLoading.value = true;
        let responseModel = await asApi.searchCommunityByAddr(queryForm.certAddress,queryForm.distance);
        tableData.value = responseModel.data;
      }
    } catch (e) {
      smartSentry.captureError(e);
    } finally {
      tableLoading.value = false;
    }
  }

  function initLP() {
    checkCityRef.value.showModal('lp');
   }
</script>
